<?php


require_once("functions.php");
$db = connect_db();

$out = get_header();

$id = isset($_GET['id']) ?$_GET['id'] : '' ;
$action = isset ($_GET['action']) ? $_GET['action'] : '';
if ($action == 'edit' && is_numeric($id)) {
	$sql = "select language from languages where id = $id";
	$res = mysql_query($sql, $db);
	if($res === FALSE)
		die ("Row not found");

	$lang = mysql_result($res, 0,0);
	if ($lang === FALSE)
		die ("Row not found");
} else if ($action == 'update' && isset($_GET['lang'])) {
	$lang = mysql_real_escape_string(strtolower($_GET['lang']));
	if (is_numeric($id))
		$sql = "update languages set language='$lang' where id=$id";
	else
		$sql = "insert into languages (language) values ('$lang')";
	
	$res = mysql_query($sql, $db);
	header("Location: view_lang.php");
} else if ($action == 'new') {
	$lang = '';
}

$out .= "<h2>Add language</h2>\n";
$out .= '<form method="get" action="edit_lang.php">'. "\n";
$out .= '<input type="hidden" name="id" value="' . $id  . "\"/>\n";
$out .= '<input type="hidden" name="action" value="update"/>' . "\n";
$out .= "language: <input type=\"text\" name=\"lang\" value=\"$lang\"/>\n";
$out .= "<br><input type=\"submit\" value=\"submit\" name=\"submit\"/>\n";
$out .= "</form>\n";


$out .= get_footer();


echo $out;


?>
